/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

 ****/
package org.activequant.dao.hibernate;

import java.util.List;

import org.activequant.core.domainmodel.InstrumentSpecification;
import org.activequant.core.domainmodel.index.IndexComponent;
import org.activequant.core.types.TimeStamp;
import org.activequant.dao.IIndexComponentDao;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;



/**
 * IndexComponentDao extends DaoSupportBase&lt;IndexComponent&gt; implements IIndexComponentDao.<br/>
 * Holds the following inherited variables:
 * <ul>
 * <li>clazz(Class&lt;IndexComponent&gt;)</li>
 * <li>hibernateTemplate(HibernateTemplate)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [10.08.2008] Created (Wessel de Roode)<br>
 *  
 *  @author Wessel de Roode 
 */
public class IndexComponentDao extends DaoSupportBase<IndexComponent> implements IIndexComponentDao  {
	/**
	 * Constructs an IndexComponentDao(extends DaoSupportBase&lt;IndexComponent&gt; implements IIndexComponentDao) whose inherited clazz(Class&lt;T&gt;) is set to <code>IndexComponent.class</code>.
	 */
	public IndexComponentDao() {
		super(IndexComponent.class);
	}
	/**
	 * Constructs an IndexComponentDao(extends DaoSupportBase&lt;IndexComponent&gt; implements IIndexComponentDao) whose inherited clazz(Class&lt;T&gt;) is set to <code>IndexComponent.class</code>.<br/>
	 * The given sessionFactory(SessionFactory) is used to set its Hibernate SessionFactory and create/initialize the inherited hibernateTemplate(HibernateTemplate) for it.
	 * @param sessionFactory
	 */
	public IndexComponentDao(SessionFactory sessionFactory) {
		super(IndexComponent.class, sessionFactory);
	}
	/**
	 * <strong>1.</strong> Returns the indexComponents(List&lt;IndexComponent&gt;) of the IndexComposition whose components(instruments) match the given spec(InstrumentSpecification) (by symbol(Symbol) and vendor(String))
	 * and whose activationTimeStamp(TimeStamp) is latest(maximum) but not after the given byThisTime(TimeStamp)
	 */
	@SuppressWarnings("unchecked")
	public List<IndexComponent> findComponentsByIndex(InstrumentSpecification spec, TimeStamp byThisTime) {

		Session sess = getSession();
		IndexCompositionDao indexCompositionDao = new IndexCompositionDao( getSessionFactory() );
		
		Long compositionId = indexCompositionDao.findByIndexSpec(spec, byThisTime).getId();
		
		String mySQL = "SELECT iComponent.*, inst.* FROM IndexComponent iComponent, InstrumentSpecification inst"+
						"  WHERE iComponent.instrumentSpecification_id = inst.id "+
						" AND iComponent.indexComposition_id = :id";
		SQLQuery query = sess.createSQLQuery(mySQL).addEntity("iComponent", IndexComponent.class).addEntity("inst", InstrumentSpecification.class);
		List<IndexComponent> list = query.setLong("id", compositionId).list();
		sess.flush();
		sess.close();
		return list;
	}
}
